package com.hzh.page.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzh.page.common.enums.RightEnum;
import com.hzh.page.common.utils.R;
import com.hzh.page.pojo.PageInfo;
import com.hzh.page.service.AuthenticationMenuService;
import com.hzh.page.service.PageInfoService;
import com.hzh.page.vo.PageInfoReqVO;
import com.hzh.page.vo.PageInfoRespVO;
import com.hzh.page.vo.PageNumVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HZH
 * @since 2022-11-11
 */
@Api(tags = "文章信息管理")
@RestController
@RequestMapping("/page/admin/pageInfo")
public class PageInfoController {

    @Autowired
    private PageInfoService pageInfoService;
    @Autowired
    private AuthenticationMenuService menuService;


    @ApiOperation("文章信息列表 -- 分页查询 -- 条件查询(标题，作者名，发布状态，审核状态，文章类别)")
    @PostMapping("/queryPageList")
    public R queryPageList(PageInfoReqVO pageInfoReqVO,
                               HttpServletRequest request) {
        menuService.hashAuthority(request, RightEnum.NEWS_MANAGE_LIST.getRoute_key());
        Page<PageInfo> pageParam = new Page<>(pageInfoReqVO.getPage(), pageInfoReqVO.getLimit());
        Page<PageInfoRespVO> queryPageList = pageInfoService.queryPageList(pageParam, pageInfoReqVO);
        return R.ok().put("queryPageList", queryPageList);
    }



    @ApiOperation("文章信息新增/修改，传入pageId则为修改，否则新增")
    @PostMapping("/addPage")
    public R addOrUpdatePage(@RequestBody PageInfo pageInfo,
                           HttpServletRequest request) {
        menuService.hashAuthority(request, RightEnum.NEWS_MANAGE_ADD.getRoute_key());
        pageInfoService.addOrUpdatePage(pageInfo);
        if (pageInfo.getPageId() == null){
            return R.ok("添加成功");
        }else {
            return R.ok("修改成功");
        }
    }

    @ApiOperation("文章信息删除")
    @GetMapping("/deletePage/{pageId}")
    public R deletePage(@PathVariable("pageId") Long pageId,
                     HttpServletRequest request) {
        menuService.hashAuthority(request, RightEnum.NEWS_MANAGE_UPDATE.getRoute_key());
        pageInfoService.removeById(pageId);
        return R.ok("删除成功");
    }

    @ApiOperation("近三十天点赞数前十的文章")
    @GetMapping("/top10Page")
    public R top10Page(HttpServletRequest request) {
        List<PageInfoRespVO> top10Page = pageInfoService.top10Page();
        return R.ok().put("top10Page",top10Page);
    }


    @ApiOperation("近三十天浏览量前十的文章")
    @GetMapping("/top10PageByView")
    public R top10PageByView(HttpServletRequest request) {
        List<PageInfoRespVO> top10PageByView = pageInfoService.top10PageByView();
        return R.ok().put("top10PageByView",top10PageByView);
    }

    @ApiOperation("获取当前登录用户能管理的未审核文章列表")
    @GetMapping("/getNoAuditPageList")
    public R queryPageList(@RequestParam Integer limit, @RequestParam Integer page, HttpServletRequest request) {
        menuService.hashAuthority(request, RightEnum.NEWS_MANAGE_LIST.getRoute_key());
        Page<PageInfo> pageParam = new Page<>(page, limit);
        Page<PageInfoRespVO> queryPageList = pageInfoService.getNoAuditPageList(pageParam, request);
        return R.ok().put("queryPageList", queryPageList);
    }

    @ApiOperation("获取每个分类下的已发布的文章的数量")
    @GetMapping("GetPublishedPagesNumOrderByCategory")
    public R GetPublishedPagesNumOrderByCategory(Integer userId, HttpServletRequest request) {
        List<PageNumVo> pageNumList = pageInfoService.GetPublishedPagesNumOrderByCategory(userId);
        return R.ok().put("pageNum",pageNumList);
    }
}

